<template>
	<view>

		<!-- 客户名称 -->
		<view class="tn-flex tn-flex-row-between tn-strip-bottom-min tn-padding">
			<view class="justify-content-item tn-flex-1">
				<view class="tn-text-bold tn-text-lg">
					{{title}}名称 <text class="tn-color-red tn-padding-left-xs">*</text>
				</view>
				<view class="tn-color-gray tn-padding-top-xs tn-color-black">
					<input v-model="formData.name" placeholder="请输入客户名称" name="name" placeholder-style="color:#AAAAAA"
						value=""></input>
				</view>
			</view>
			<view class="justify-content-item tn-text-xl tn-color-grey tn-margin-left">
				<view class="tn-icon-user tn-padding-top"></view>
			</view>
		</view>

		<!-- 客户来源 -->
		<picker @change="bindSourceChange" :value="sourceIndex" :range="DICT.crm_customer_source" range-key="label">
			<view class="tn-flex tn-flex-row-between tn-strip-bottom-min tn-padding">
				<view class="justify-content-item tn-flex-1">
					<view class="tn-text-bold tn-text-lg">
						客户来源 <text class="tn-color-red tn-padding-left-xs">*</text>
					</view>
					<view class="tn-color-gray tn-padding-top-xs" v-if="sourceIndex===99">
						请选择
					</view>
					<view class="tn-color-black tn-padding-top-xs" v-else>
						{{DICT.crm_customer_source[sourceIndex].label}}
					</view>
				</view>
				<view class="justify-content-item tn-text-lg tn-color-grey tn-margin-left">
					<view class="tn-icon-right tn-padding-top"></view>
				</view>
			</view>
		</picker>

		<!-- 手机号码 -->
		<view class="tn-flex tn-flex-row-between tn-strip-bottom-min tn-padding">
			<view class="justify-content-item tn-flex-1">
				<view class="tn-text-bold tn-text-lg">
					手机号码
				</view>
				<view class="tn-color-gray tn-padding-top-xs tn-color-black">
					<input v-model="formData.mobile" placeholder="请输入手机号码" name="mobile"
						placeholder-style="color:#AAAAAA" value=""></input>
				</view>
			</view>
			<view class="justify-content-item tn-text-xl tn-color-grey tn-margin-left">
				<view class="tn-icon-phone tn-padding-top"></view>
			</view>
		</view>

		<!-- 固定电话 -->
		<view class="tn-flex tn-flex-row-between tn-strip-bottom-min tn-padding">
			<view class="justify-content-item tn-flex-1">
				<view class="tn-text-bold tn-text-lg">
					固定电话
				</view>
				<view class="tn-color-gray tn-padding-top-xs tn-color-black">
					<input v-model="formData.telephone" placeholder="请输入固定电话" name="telephone"
						placeholder-style="color:#AAAAAA" value=""></input>
				</view>
			</view>
			<view class="justify-content-item tn-text-xl tn-color-grey tn-margin-left">
				<view class="tn-icon-phone tn-padding-top"></view>
			</view>
		</view>

		<!-- 邮箱 -->
		<view class="tn-flex tn-flex-row-between tn-strip-bottom-min tn-padding">
			<view class="justify-content-item tn-flex-1">
				<view class="tn-text-bold tn-text-lg">
					邮箱地址
				</view>
				<view class="tn-color-gray tn-padding-top-xs tn-color-black">
					<input v-model="formData.email" placeholder="请输入邮箱地址" name="email" placeholder-style="color:#AAAAAA"
						value=""></input>
				</view>
			</view>
			<view class="justify-content-item tn-text-xl tn-color-grey tn-margin-left">
				<view class="tn-icon-mail tn-padding-top"></view>
			</view>
		</view>

		<!-- 客户级别 -->
		<picker @change="bindLevelChange" :value="levelIndex" :range="DICT.crm_customer_level" range-key="label">
			<view class="tn-flex tn-flex-row-between tn-strip-bottom-min tn-padding">
				<view class="justify-content-item tn-flex-1">
					<view class="tn-text-bold tn-text-lg">
						客户级别
					</view>
					<view class="tn-color-gray tn-padding-top-xs" v-if="levelIndex===99">
						请选择
					</view>
					<view class="tn-color-black tn-padding-top-xs" v-else>
						{{DICT.crm_customer_level[levelIndex].label}}
					</view>
				</view>
				<view class="justify-content-item tn-text-lg tn-color-grey tn-margin-left">
					<view class="tn-icon-right tn-padding-top"></view>
				</view>
			</view>
		</picker>

		<!-- 客户行业 -->
		<picker @change="bindIndustryChange" :value="industryIndex" :range="DICT.crm_customer_industry"
			range-key="label">
			<view class="tn-flex tn-flex-row-between tn-strip-bottom-min tn-padding">
				<view class="justify-content-item tn-flex-1">
					<view class="tn-text-bold tn-text-lg">
						客户行业
					</view>
					<view class="tn-color-gray tn-padding-top-xs" v-if="industryIndex===99">
						请选择
					</view>
					<view class="tn-color-black tn-padding-top-xs" v-else>
						{{DICT.crm_customer_industry[industryIndex].label}}
					</view>
				</view>
				<view class="justify-content-item tn-text-lg tn-color-grey tn-margin-left">
					<view class="tn-icon-right tn-padding-top"></view>
				</view>
			</view>
		</picker>

		<!-- 下次联系时间 -->
		<picker mode="date" :value="formData.contactNextTime" :start="startDate" :end="endDate" @change="setNextTime">
			<view class="tn-flex tn-flex-row-between tn-strip-bottom-min tn-padding">
				<view class="justify-content-item tn-flex-1">
					<view class="tn-text-bold tn-text-lg">
						下次联系时间
					</view>
					<view class="tn-color-gray tn-padding-top-xs" v-if="formData.contactNextTime===''">
						请选择
					</view>
					<view class="tn-padding-top-xs" v-else>
						{{ formData.contactNextTime }}
					</view>
				</view>
				<view class="justify-content-item tn-text-lg tn-color-grey tn-margin-left">
					<view class="tn-icon-time tn-padding-top"></view>
				</view>
			</view>
		</picker>

		<!-- 地址 -->
		<view class="tn-flex tn-flex-row-between tn-strip-bottom-min tn-padding" @click="showArea = true">
			<view class="justify-content-item tn-flex-1">
				<view class="tn-text-bold tn-text-lg">
					地区
				</view>
				<view class="tn-color-gray tn-padding-top-xs tn-color-black">
					{{areaTreeName}}


				</view>
			</view>
			<view class="justify-content-item tn-text-xl tn-color-grey tn-margin-left">
				<view class="tn-icon-location tn-padding-top"></view>
			</view>
		</view><!-- 地址 -->

		<view class="tn-flex tn-flex-row-between tn-strip-bottom-min tn-padding">
			<view class="justify-content-item tn-flex-1">
				<view class="tn-text-bold tn-text-lg">
					详细地址
				</view>
				<view class="tn-color-gray tn-padding-top-xs tn-color-black">
					<input v-model="formData.detailAddress" placeholder="请输入详细地址" name="detailAddress"
						placeholder-style="color:#AAAAAA" value=""></input>
				</view>
			</view>
			<view class="justify-content-item tn-text-xl tn-color-grey tn-margin-left">
				<view class="tn-icon-location tn-padding-top"></view>
			</view>
		</view>

		<!-- 备注 -->
		<view class="tn-flex tn-flex-row-between tn-flex-col-center tn-padding">
			<view class="tn-flex justify-content-item">
				<view class="tn-text-bold tn-text-lg">
					备注信息
				</view>
			</view>
			<view class="tn-flex justify-content-item tn-color-gray">
				200字内
			</view>
		</view>
		<view class="tn-bg-gray--light tn-padding tn-text-justify"
			style="border-radius: 10rpx;margin: 0 30rpx 30rpx 30rpx;">
			<textarea v-model="formData.remark" maxlength="200" placeholder="请输入备注信息" placeholder-style="color:#AAAAAA"
				style="height: 160rpx;width: 100%;"></textarea>
		</view>


		<!-- 悬浮按钮-->
		<view class="tn-flex tn-footerfixed tn-padding">
			<view class="tn-flex-1 justify-content-item tn-text-center">
				<tn-button backgroundColor="#00C8B0 " padding="40rpx 0" width="60%" :fontSize="28" fontColor="#FFFFFF"
					shape="round" @click="submitForm">
					<text class="">提交保存</text>
				</tn-button>
			</view>
		</view>
		<tn-select labelName="name" childName="children" valueName="id" v-model="showArea" mode="multi-auto"
			:list="areaTree" @confirm="confirmAreaTree"></tn-select>
	</view>
</template>

<script>
	import template_page_mixin from '@/libs/mixin/template_page_mixin.js'

	export default {
		mixins: [template_page_mixin],
		props: {
			title: {
				type: String,
				default: ''
			},
			list: {
				type: Array,
				default () {
					return []
				}
			}
		},
		data() {
			return {
				showArea: false,
				// 日期选择器配置
				startDate: '2023-01-01',
				endDate: '2100-12-31',

				// 客户来源选择
				sourceIndex: 99,

				// 客户级别选择
				levelIndex: 99,

				// 客户行业选择
				industryIndex: 99,

				// 锁定状态选择
				lockStatusIndex: 99,

				// 成交状态选择
				dealStatusIndex: 99,
				formData: {
					name: '', // 客户名称
					source: '', // 客户来源
					mobile: '', // 手机号码
					telephone: '', // 固定电话
					email: '', // 邮箱地址
					level: '', // 客户级别
					industryId: '', // 客户行业
					contactNextTime: '', // 下次联系时间
					detailAddress: '', // 详细地址
					remark: '', // 备注信息
					lockStatus: '', // 锁定状态
					dealStatus: '', // 成交状态
					areaId: ''
				},
				areaTree: [],
				areaTreeName: ''
			}
		},
		mounted() {
			let that = this
			this.$store.dispatch('getAreaTreeData').then((r) => {
				that.areaTree = r
				console.log(r)

			})
		},
		methods: {
			confirmAreaTree(e) {
				this.areaTreeName = e[0].label + ',' + e[1].label + ',' + e[2].label
				this.areaId = e[2].id
				console.log('e', e)
			},
			// 客户来源选择
			bindSourceChange(e) {
				this.sourceIndex = e.detail.value
				this.formData.source = this.DICT.crm_customer_source[e.detail.value].value
			},

			// 客户级别选择
			bindLevelChange(e) {
				this.levelIndex = e.detail.value
				this.formData.level = this.DICT.crm_customer_level[e.detail.value].value
			},

			// 客户行业选择
			bindIndustryChange(e) {
				this.industryIndex = e.detail.value
				this.formData.industryId = this.DICT.crm_customer_industry[e.detail.value].value
			},

			// 设置下次联系时间
			setNextTime(e) {
				this.formData.contactNextTime = e.detail.value
			},

			// 锁定状态选择
			bindLockStatusChange(e) {
				this.lockStatusIndex = e.detail.value
				this.formData.lockStatus = this.DICT.infra_boolean_string[e.detail.value].value
			},

			// 成交状态选择
			bindDealStatusChange(e) {
				this.dealStatusIndex = e.detail.value
				this.formData.dealStatus = this.DICT.infra_boolean_string[e.detail.value].value
			},

			submitForm() {
				const submitData = {
					name: this.formData.name.trim(),
					source: this.formData.source,
					mobile: this.formData.mobile || null,
					telephone: this.formData.telephone || null,
					email: this.formData.email || null,
					level: this.formData.level || null,
					industryId: this.formData.industryId || null,
					contactNextTime: this.formData.contactNextTime ? new Date(this.formData.contactNextTime +
							'T00:00:00')
						.getTime() : null,
					detailAddress: this.formData.detailAddress || null,
					remark: this.formData.remark || null,
					lockStatus: this.formData.lockStatus || '0',
					dealStatus: this.formData.dealStatus || '0',
					ownerUserId: this.USER_ID
				}
				this.$emit('submitForm', submitData)
			}
		}
	}
</script>

<style>
</style>